//
//  WMMusicRequestQQ.m
//  WholeMusic
//
//  Created by quanxiong on 2018/2/23.
//  Copyright © 2018年 quanxiong. All rights reserved.
//

#import "WMMusicRequestQQ.h"

static NSString *const s_userAgent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) " \
"AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1";

static NSString *const s_GUID = @"5150825362";

@interface WMMusicRequestQQ ()

@end

@implementation WMMusicRequestQQ

- (AFHTTPSessionManager *)searchManager {
    AFHTTPSessionManager *manager = [self managerForBaseURL:@"http://c.y.qq.com"];
    [manager.requestSerializer setValue:@"http://m.y.qq.com" forHTTPHeaderField:@"Referrer"];
    [manager.requestSerializer setValue:s_userAgent forHTTPHeaderField:@"User-Agent"];
    return manager;
}

- (AFHTTPSessionManager *)updateVKeyManager {
    AFHTTPSessionManager *manager = [self managerForBaseURL:@"http://base.music.qq.com"];
    [manager.requestSerializer setValue:@"http://y.qq.com" forHTTPHeaderField:@"Referrer"];
    [manager.requestSerializer setValue:s_userAgent forHTTPHeaderField:@"User-Agent"];
    return manager;
}

- (void)searchMusicWithKeyaord:(NSString *)keyword complete:(void(^)(id response))complete {
    if (keyword.length == 0) {
        if (complete) {
            complete(nil);
        }
        return;
    }
    //TODO: 分页参数
    NSInteger pageSize = 50;
    NSInteger page = 0;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters addEntriesFromDictionary:@{@"p": @(page),
                                           @"n": @(pageSize),
                                           @"format": @"json",
                                           @"w": keyword
                                           }];
    [self.searchManager GET:@"/soso/fcgi-bin/search_for_qq_cp"
                 parameters:parameters
                   progress:nil
                    success:
     ^(NSURLSessionDataTask *task, id responseObject) {
         NSLog(@"%@", responseObject);
         if (complete) {
             complete(responseObject);
         }
     } failure:^(NSURLSessionDataTask * task, NSError * error) {
         NSLog(@"%@", error);
         if (complete) {
             complete(nil);
         }
     }];
}

- (void)getVKeyWithComplete:(void(^)(id response))complete {

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters addEntriesFromDictionary:@{@"json": @"0",
                                           @"guid": s_GUID,
                                           @"format": @"json",
                                           }];
    [[self updateVKeyManager] GET:@"/fcgi-bin/fcg_musicexpress.fcg"
                       parameters:parameters
                         progress:nil
                          success:
     ^(NSURLSessionDataTask * task, id responseObject) {
        NSLog(@"%@", responseObject);
        if (complete) {
            complete(responseObject);
        }
    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        NSLog(@"%@", error);
        if (complete) {
            complete(nil);
        }
    }];
}

@end
